package com.ihelp.rest.mis.platform.controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.ihelp.rest.mis.platform.service.UserRecordService;
import com.ihelp.rest.mis.pojo.SysUser;
import com.ihelp.rest.mis.pojo.UserRecord;
import com.ihelp.rest.mis.pojo.Users;
import com.ihelp.rest.mis.users.service.UsersService;
import com.ihelp.rest.utils.HtmlUtil;
import com.ihelp.rest.utils.SessionUtils;


@Controller
@RequestMapping("/system/record")
public class UserRecordController {

	@Autowired
	private UserRecordService urservice;
	@Autowired
	private UsersService usersService;
	
	@RequestMapping("/recordManage.do")
	public String loadSiteTypeManage(HttpServletRequest request){
		return "system/record/recordManage";
	}
	
	@RequestMapping("/ycRecord.do")
	public String ycRecord(HttpServletRequest request){
		return "system/record/ycRecord";
	}
	
	/**
	 * 新增单条记录
	 * */
	@RequestMapping("/recordAdd.do")
	public String recordAdd(HttpServletRequest request,String id){
		if(StringUtils.isNotBlank(id)){
			request.setAttribute("UserRecord", this.urservice.getUserRecordById(id));
		}
		return "system/record/recordAdd";
	}
	
	/**
	 * 上网记录列表
	 * @param request
	 */
	@RequestMapping("/recordList.do")
	public void recordList(HttpServletResponse response,int page, int rows ,String computername,String qtime ){
		String json = this.urservice.getRecordList(page,rows,computername,qtime).toString();
		HtmlUtil.writerJson(response, json);
	}
	/**
	 * 保存记录列表单条  
	 * @param response
	 */
	@RequestMapping("/saveRecord.do")
	public void saveRecord(Model model,HttpServletResponse response, HttpSession session, UserRecord ur){
		String result = new String(); 
		result= this.urservice.saveRecord(ur); 
		HtmlUtil.sendDirectToClient(response,result);
	}
	
	
	@RequestMapping("/saveRecords.do")
	public void saveRecords(HttpServletResponse response,HttpServletRequest request, MultipartFile records) throws FileUploadException, IOException{
		String result = new String();  
		FileInputStream file = (FileInputStream) records.getInputStream();
		result = this.urservice.saveRecords(file);
		HtmlUtil.writerHtml(response,result);
	} 

	/**
	 * 删除记录
	 * @param response
	 * @param module
	 */
	@RequestMapping("/delRecord.do")
	public void delRecord(HttpServletResponse response, String id){
		String result = this.urservice.delRecord(id);
		HtmlUtil.sendDirectToClient(response,result);
	}
	
	/**
	 * 异常上网记录列表
	 * @param request
	 */
	@RequestMapping("/recordYcList.do")
	public void recordYcList(Model model,HttpServletResponse response,String usercode,String begin,String end,int page, int rows ){
		String json = this.urservice.getYCSWByUsercode( page,  rows,usercode,begin,end).toString();
		HtmlUtil.writerJson(response, json);
	}
	
	@RequestMapping("/recordCXqx.do")
	public void delMSiteType(HttpServletRequest request,HttpServletResponse response, String usercode){
		SysUser user = SessionUtils.getUser(request);
		Map<String ,Object >  result = new HashMap<String, Object>();
		Users hy = new Users();
		hy = this.usersService.getUsersByCode(usercode);
		if(hy==null){
			result.put("data", "会员不存在！");
			result.put("success", "false");
		}else if(user.getLoginName().equals("system")||hy.getSaleUser().getUuid().equals(user.getUuid())||hy.getManagerUser().getUuid().equals(user.getUuid())||hy.getAssistantUser().getUuid().equals(user.getUuid())){
			if(StringUtils.isBlank(hy.getBirthday())){
				result.put("success", "false");
				result.put("data", "此会员未设定出生日期！");
			}else{
				result.put("success", "true");
			}
		}else{
			result.put("data", "您无此会员的查询权限！");
			result.put("success", "false");
		}
		HtmlUtil.sendDirectToClient(response,JSONObject.fromObject(result).toString());
	}
	
	/**
	 *上网记录分析
	 * @param request
	 */
	@RequestMapping("/recordFxb.do")
	public void recordFxb(Model model,HttpServletResponse response,String usercode ,String begin,String end ){
		List<Map<String, Object>> fxb =this.urservice.getFxbByUsercode(usercode ,begin,end);
		HtmlUtil.writerJson(response, JSONArray.fromObject(fxb).toString());
	}
	
	/**
	 * 导出异常上网记录
	 * @param userCode
	 */
	@RequestMapping("/exportYcRecord.do")
	public void exportYcRecord(HttpServletRequest request, HttpServletResponse response, String userCode,String begin,String end) {
		
		/*String fxb_encoding;	
		String code =  getEncoding(fxb); 
		if(code.equals("ISO-8859-1")){
			try{
				fxb_encoding = new String(fxb.getBytes("ISO-8859-1"),"gbk");
	        }catch (Exception e) {
	        	fxb_encoding = fxb;
	        }
		}else{
			fxb_encoding = fxb;
		} */
		List<Map<String, Object>> fxb = this.urservice.getFxbByUsercode(userCode ,begin,end);
        
		String result = this.urservice.exportRecord(request, response,userCode,
										this.urservice.getYCSWByUsercode(userCode,begin,end),fxb,begin,end);
		HtmlUtil.sendDirectToClient(response,result);
	}
	
	public static String getEncoding(String str) {    
        String encode = "GB2312";    
       try {    
           if (str.equals(new String(str.getBytes(encode), encode))) {    
                String s = encode;    
               return s;    
            }    
        } catch (Exception exception) {    
        }    
        encode = "ISO-8859-1";    
       try {    
           if (str.equals(new String(str.getBytes(encode), encode))) {    
                String s1 = encode;    
               return s1;    
            }    
        } catch (Exception exception1) {    
        }    
        encode = "UTF-8";    
       try {    
           if (str.equals(new String(str.getBytes(encode), encode))) {    
                String s2 = encode;    
               return s2;    
            }    
        } catch (Exception exception2) {    
        }    
        encode = "GBK";    
       try {    
           if (str.equals(new String(str.getBytes(encode), encode))) {    
                String s3 = encode;    
               return s3;    
            }    
        } catch (Exception exception3) {    
        }    
       return "";    
    }  
}
